/**
 * 标本送检记录管理初始化
 */
var LabVisit = {
    id: "LabVisitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
LabVisit.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '就诊记录', field: 'episodeId', align: 'center', valign: 'middle', sortable: true},
        {title: '索引码', field: 'xcode', align: 'center', valign: 'middle', sortable: true},
        {title: '检验号|标本号', field: 'specVisitNum', align: 'center', valign: 'middle', sortable: true},
        {title: '标本', field: 'specimen', align: 'center', valign: 'middle', sortable: true},
        {title: '申请科室|送检科室', field: 'labLocId', align: 'center', valign: 'middle', sortable: true},
        {title: '申请病区|送检病区', field: 'labWardId', align: 'center', valign: 'middle', sortable: true},
        {title: '采集日期', field: 'colDate', align: 'center', valign: 'middle', sortable: true},
        {title: '采集人', field: 'colUserName', align: 'center', valign: 'middle', sortable: true},
        {title: '接收日期', field: 'recDate', align: 'center', valign: 'middle', sortable: true},
        {title: '接受人', field: 'recUserName', align: 'center', valign: 'middle', sortable: true},
        {title: '元数据', field: 'dataId', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:LabVisit.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:LabVisit.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
LabVisit.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        LabVisit.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增标本送检记录
 */
LabVisit.add = function () {
    var index = Sys.open('新增标本送检记录',Sys.backendPath + '/cdc/labvisit/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看标本送检记录详情
 */
LabVisit.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('标本送检记录详情',Sys.backendPath + '/cdc/labvisit/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除标本送检记录
 */
LabVisit.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/labvisit/delete", function (data) {
	            Sys.success("删除成功!");
	            LabVisit.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("labVisitId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除标本送检记录?",operation);
    }
};

/**
 * 查询标本送检记录列表
 */
LabVisit.search = function () {
    var queryData = {};
    queryData['xcode'] = $("#xcode").val();
    queryData['specVisitNum'] = $("#specVisitNum").val();
    queryData['specimen'] = $("#specimen").val();
    queryData['labLocId'] = $("#labLocId").val();
    queryData['labWardId'] = $("#labWardId").val();
    queryData['colDate'] = $("#colDate").val();
    queryData['colUserName'] = $("#colUserName").val();
    queryData['recDate'] = $("#recDate").val();
    queryData['recUserName'] = $("#recUserName").val();
    queryData['dataId'] = $("#dataId").val();
    queryData['isActive'] = $("#isActive").val();
    LabVisit.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = LabVisit.initColumn();
    var table = new BSTable(LabVisit.id, Sys.backendPath + "/cdc/labvisit/list", defaultColunms);
    table.setPaginationType("server");
    LabVisit.table = table.init();
});
